//
//  HTDeliveredVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/16.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTDeliveredVC.h"
#import "HTDeliverdCell.h"
#import "UIView+Layout.h"
#import "HTDeliverDetialVC.h"
#import "HTDeliverGoodsModel.h"

@interface HTDeliveredVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *tapLeft;
@property (weak, nonatomic) IBOutlet UIView *lineLeft;
@property (weak, nonatomic) IBOutlet UILabel *labelLeft;

@property (weak, nonatomic) IBOutlet UIView *tapRight;
@property (weak, nonatomic) IBOutlet UIView *lineRight;
@property (weak, nonatomic) IBOutlet UILabel *labelRight;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@property (nonatomic,weak) UIView *selectedView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation HTDeliveredVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (void)initData{
    [self setupHeadView];
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT + kHTToolBarHeight + IPHONE_NAVIGATIONBAR_HEIGHT;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTDeliverdCell" bundle:nil] forCellReuseIdentifier:DeliveredCell];
    [self configTableHeader];
    self.tableView.mj_header = self.header;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"你尚未保存任何常发货源";
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark --- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.selectedView == self.tapLeft) {
         self.noDataView.showMessage = @"发布中的资源将在这里显示";
    }else{
        self.noDataView.showMessage = @"被关闭/已成交的货源，将在这里显示";
    }
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTDeliverdCell *cell =(HTDeliverdCell *)[tableView dequeueReusableCellWithIdentifier:DeliveredCell forIndexPath:indexPath withController:self];
    cell.deliverGoodsModel = self.dataSource[indexPath.row];
    [cell setRefreshBlock:^{
        [tableView.mj_header beginRefreshing];
    }];
    if (self.selectedView == self.tapLeft) {
        cell.isRuning = YES;
    }else{
        cell.isRuning = NO;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTDeliverDetialVC *deliverDetial = [[HTDeliverDetialVC alloc]init];
    deliverDetial.deliverGoodsModel = self.dataSource[indexPath.row];
    if (self.selectedView == _tapLeft) {
        deliverDetial.isClosed = NO;
    }else{
         deliverDetial.isClosed = YES;
    }
    [self.navigationController pushViewController:deliverDetial animated:YES];
}

#pragma mark --- 点击事件
- (void)tapGesture:(UITapGestureRecognizer *)sender{
    
    if(sender.view.tag == 121){//left
        if (self.selectedView == self.tapLeft) {
            return;
        }
        self.selectedView = self.tapLeft;
        [self selectedLeft];
        [self.tableView.mj_header beginRefreshing];
    }else{//right
        if (self.selectedView == self.tapRight) {
            return;
        }
        self.selectedView = self.tapRight;
        [self selectedRight];
        [self.tableView.mj_header beginRefreshing];
    }
    
}
#pragma mark --- 网络请求
- (void)loadNewData{
    if (self.selectedView == self.tapLeft) {
        [self netLoadDataWithState:@"0"];
    }
    if (self.selectedView == self.tapRight) {
        [self netLoadDataWithState:@"1"];
    }
}
//0 发布中 1 已关闭
- (void)netLoadDataWithState:(NSString *)state{
    [HTHttpRequestManager requestDeliverGoodsListWithState:state success:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        DBLog(@"发货列表：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取列表失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"获取列表失败"];
            return;
        }
        self.dataSource = [HTDeliverGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma mark --- headView
- (void)setupHeadView{
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    self.tapLeft.tag = 121;
    [self.tapLeft addGestureRecognizer:tap1];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    self.tapRight.tag = 122;
    [self.tapRight addGestureRecognizer:tap2];
    self.selectedView = self.tapLeft;
    [self.lineLeft setHidden:NO];
    [self.lineRight setHidden:YES];
    self.labelLeft.textColor = kBlueColor;
    self.labelRight.textColor = [UIColor darkGrayColor];
}
- (void)selectedLeft{
    [self.lineLeft setHidden:NO];
    [self.lineRight setHidden:YES];
    self.labelLeft.textColor = kBlueColor;
    self.labelRight.textColor = [UIColor darkGrayColor];
}
- (void)selectedRight{
    [self.lineLeft setHidden:YES];
    [self.lineRight setHidden:NO];
    self.labelLeft.textColor = [UIColor darkGrayColor];
    self.labelRight.textColor = kBlueColor;
}
@end
